<template>
  <div class="csv-graph-view">
    <div class="page-header">
      <h1>📊 CSV数据图谱分析</h1>
      <p class="page-description">
        数据关系图谱分析，可视化展示列之间的相关性，支持关系管理
      </p>
    </div>

    <div class="content-container">
      <CsvGraphUploader 
        ref="csvUploader"
        @graph-generated="handleGraphGenerated"
        @graph-updated="handleGraphUpdated"
        @processing-completed="handleProcessingCompleted"
      />
    </div>

    <div v-if="showInstructions" class="instructions-section">
      <h3>📋 使用说明</h3>
      <div class="instructions-grid">
        <div class="instruction-card">
          <div class="instruction-icon">🚀</div>
          <h4>启动后端服务</h4>
          <p>首先启动Graph Backend服务：</p>
          <ul>
            <li>进入 <code>Graph_backend</code> 目录</li>
            <li>运行 <code>start_server.bat</code> (Windows) 或 <code>python start_server.py</code></li>
            <li>确保服务运行在 <code>http://localhost:5000</code></li>
          </ul>
        </div>

        <div class="instruction-card">
          <div class="instruction-icon">📊</div>
          <h4>图谱分析</h4>
          <p>系统会自动分析数据：</p>
          <ul>
            <li>解析CSV数据结构</li>
            <li>计算列之间的相关性</li>
            <li>生成节点和连线数据</li>
            <li>展示数据统计信息</li>
          </ul>
        </div>

        <div class="instruction-card">
          <div class="instruction-icon">🔗</div>
          <h4>关系管理</h4>
          <p>您可以管理图谱关系：</p>
          <ul>
            <li>添加新的节点关系</li>
            <li>修改现有关系权重</li>
            <li>删除不需要的关系</li>
            <li>实时更新图谱显示</li>
          </ul>
        </div>

        <div class="instruction-card">
          <div class="instruction-icon">🎯</div>
          <h4>图谱解读</h4>
          <p>图谱中的元素含义：</p>
          <ul>
            <li><strong>节点</strong>：代表CSV中的列，大小反映数据重要性</li>
            <li><strong>连线</strong>：表示列之间的相关性，粗细反映相关强度</li>
            <li><strong>颜色</strong>：不同颜色代表不同的数据类型分组</li>
            <li><strong>统计</strong>：显示数据概览和缺失情况</li>
          </ul>
        </div>
      </div>
    </div>

    <div class="toggle-instructions">
      <button @click="toggleInstructions" class="btn btn-outline">
        {{ showInstructions ? '隐藏说明' : '显示说明' }}
      </button>
    </div>
  </div>
</template>

<script>
import CsvGraphUploader from '@/components/3d-network/CsvGraphUploader.vue'

export default {
  name: 'CsvGraphView',
  components: {
    CsvGraphUploader
  },
  data() {
    return {
      showInstructions: false,
      hasStoredData: false
    }
  },
  mounted() {
    // 检查是否有存储的数据
    this.checkStoredData()
  },
  methods: {
    checkStoredData() {
      const storedData = sessionStorage.getItem('csvGraphData')
      if (storedData) {
        try {
          const graphData = JSON.parse(storedData)
          if (graphData && graphData.nodes && graphData.nodes.length > 0) {
            this.hasStoredData = true
            // 通知子组件有数据可用
            this.$nextTick(() => {
              if (this.$refs.csvUploader) {
                this.$refs.csvUploader.loadStoredData(graphData)
              }
            })
          }
        } catch (error) {
          console.error('解析存储的数据时出错:', error)
        }
      }
    },

    handleGraphGenerated(graphData) {
      console.log('图谱数据已生成:', graphData)
      this.hasStoredData = true
      // 存储数据到sessionStorage
      sessionStorage.setItem('csvGraphData', JSON.stringify(graphData))
    },

    handleGraphUpdated(graphData) {
      console.log('图谱数据已更新:', graphData)
      // 更新存储的数据
      sessionStorage.setItem('csvGraphData', JSON.stringify(graphData))
    },

    handleProcessingCompleted(event) {
      console.log('处理完成事件:', event)
      console.log('事件类型:', event.type)
      console.log('是否应该跳转:', event.shouldNavigate)
      
      if (event.type === 'success' && event.shouldNavigate) {
        console.log('准备跳转到 /graph-management')
        
        // 存储新的图谱数据
        if (event.newGraphData) {
          sessionStorage.setItem('csvGraphData', JSON.stringify(event.newGraphData))
          console.log('已存储新的图谱数据')
        }
        
        // 延迟跳转，让用户看到成功消息
        setTimeout(() => {
          console.log('执行页面跳转...')
          this.$router.push({
            path: '/graph-management',
            query: {
              fromCsv: 'true',
              hasNewData: 'true'
            }
          })
        }, 2000)
      } else {
        console.log('不满足跳转条件:', {
          type: event.type,
          shouldNavigate: event.shouldNavigate
        })
      }
    },

    toggleInstructions() {
      this.showInstructions = !this.showInstructions
    }
  }
}
</script>

<style scoped>
.csv-graph-view {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.page-header {
  text-align: center;
  margin-bottom: 30px;
  color: white;
}

.page-header h1 {
  font-size: 2.5rem;
  margin-bottom: 10px;
  text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
}

.page-description {
  font-size: 1.1rem;
  opacity: 0.9;
  max-width: 600px;
  margin: 0 auto;
}

.content-container {
  background: white;
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.2);
  overflow: hidden;
  margin-bottom: 30px;
}

.instructions-section {
  background: white;
  border-radius: 12px;
  padding: 30px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.2);
  margin-bottom: 20px;
}

.instructions-section h3 {
  text-align: center;
  margin-bottom: 30px;
  color: #333;
  font-size: 1.5rem;
}

.instructions-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 20px;
}

.instruction-card {
  background: #f8f9fa;
  border-radius: 8px;
  padding: 20px;
  border-left: 4px solid #007bff;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.instruction-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}

.instruction-icon {
  font-size: 2rem;
  margin-bottom: 15px;
}

.instruction-card h4 {
  margin-bottom: 10px;
  color: #333;
}

.instruction-card p {
  color: #666;
  margin-bottom: 10px;
}

.instruction-card ul {
  margin: 0;
  padding-left: 20px;
}

.instruction-card li {
  margin-bottom: 5px;
  color: #555;
}

.instruction-card code {
  background: #e9ecef;
  padding: 2px 6px;
  border-radius: 3px;
  font-family: 'Courier New', monospace;
  color: #d63384;
}

.toggle-instructions {
  text-align: center;
}

.btn {
  padding: 10px 20px;
  border: 2px solid white;
  border-radius: 25px;
  background: transparent;
  color: white;
  cursor: pointer;
  font-size: 16px;
  transition: all 0.3s ease;
}

.btn:hover {
  background: white;
  color: #667eea;
}

.btn-outline {
  border-color: white;
  background: transparent;
}

.btn-outline:hover {
  background: white;
  color: #667eea;
}

@media (max-width: 768px) {
  .csv-graph-view {
    padding: 10px;
  }
  
  .page-header h1 {
    font-size: 2rem;
  }
  
  .page-description {
    font-size: 1rem;
  }
  
  .instructions-grid {
    grid-template-columns: 1fr;
  }
  
  .instructions-section {
    padding: 20px;
  }
}
</style>
